﻿using ByHand.Model;
using ByHand.Services.Code;
using ByHand.Services.Implements;
using ByHand.Services.Interfaces;
using System;
using System.Configuration;
using System.Linq;
using System.Web.Mvc;

namespace FrontEnd.Controllers
{
    public class PaymentController : Controller
    {
        private readonly IProductService _ProductService;
        private readonly IProviderService _ProviderService;
        private readonly IOrderService _OrderService;
        private readonly IOrderDetailService _OrderDetailService;
        private readonly ILogService _LogService;
        private readonly IProductOptionService _ProductOptionService;
        private readonly IUserService _UserService;

        public PaymentController(IProductService ProductService, IProviderService ProviderService, ILogService LogService, IOrderDetailService OrderDetailService, IOrderService OrderService, IProductOptionService ProductOPtionService, IUserService UserService)
        {
            _ProductService = ProductService;
            _ProviderService = ProviderService;
            _LogService = LogService;
            _OrderDetailService = OrderDetailService;
            _OrderService = OrderService;
            _ProductOptionService = ProductOPtionService;
            _UserService = UserService;
        }

        public ActionResult WidgetPopup(int id, string tempID, string refid, string opentype, string location)
        {
            if (!string.IsNullOrWhiteSpace(opentype))
            {
                var log = new Log();
                log.DateTime = DateTime.Now;
                log.UserName = tempID;
                log.UserType = "Customer";
                log.ProductID = id;
                log.OrderID = 0;
                log.RefID = refid;
                log.Location = location;
                var userlog = "";
                if (User.Identity.IsAuthenticated) userlog = User.Identity.Name;
                log.ActionDetail = (int)ELogActionDetail.None;
                log.ByHandName = userlog;
                if (opentype.Equals("1")) { log.ActionType = (int)ELogActionType.Button_Html_Click; }
                else if (opentype.Equals("2")) { log.ActionType = (int)ELogActionType.Button_Widget_Click; }
                else log.ActionType = (int)ELogActionType.ByHand_Click;
                _LogService.Add(log);
            }
            var model = _ProductService.GetById(id);
            model.Provider = _ProviderService.GetById(model.ProviderID);
            ViewData["options"] = _ProductOptionService.GetByProductID(model.ID);
            ViewData["tempID"] = tempID;
            ViewData["refid"] = refid;
            ViewData["reflocation"] = location;
            if (User.Identity.IsAuthenticated)
            {
                ViewData["user"] = _UserService.GetByFBID(User.Identity.Name) ?? _UserService.GetByEmail(User.Identity.Name);
            }
            return PartialView(model);
        }

        public ActionResult WidgetEmbed(string width, string height, string id)
        {
            //ViewData["width"] = width;
            //ViewData["height"] = height;
            var product = _ProductService.GetById(Convert.ToInt32(id));
            return PartialView(product);
        }

        public ActionResult RequestPayment()
        {
            var title = Request.Form["title"];
            var code = Request.Form["code"];
            var price = Convert.ToInt64(Request.Form["price"]);
            var email = Request.Form["email"];
            var phone = Request.Form["phone"];
            var details = Request.Form["details"];
            string returnURL = string.Format("http://{0}/{1}", System.Web.HttpContext.Current.Request.Url.Host,
                                             ConfigurationManager.AppSettings["PG_RETURN_URL"]);
            string urlcheckout = "";
            SohaPayment pay = new SohaPayment();
            urlcheckout = pay.BuildCheckoutUrl(returnURL, title, code, price, email, phone, details);
            return Redirect(urlcheckout);
        }

        public ActionResult ButtonShared(int id, string refid)
        {
            ViewData["productid"] = id;
            ViewData["refid"] = refid;
            return PartialView();
        }

    }
}